Matrix Arithmetic

We can perform element by element mathematical operations on a matrix with a scalar (single number) just like we could with vectors. Let's see some quick examples:

In [3]:
mat <- matrix(1:50,byrow=TRUE,nrow=5)
mat
Out[3]:
1 2 3 4 5 6 7 8 910
11121314151617181920
21222324252627282930
31323334353637383940
41424344454647484950
In [2]:
# Multiplication
2*mat
Out[2]:
2 4 6 8101214161820
22242628303234363840
42444648505254565860
62646668707274767880
82 84 86 88 90 92 94 96 98100
In [4]:
# Division (reciprocal)
1/mat
Out[4]:
1.00000000.50000000.33333330.25000000.20000000.16666670.14285710.12500000.11111110.1000000
0.090909090.083333330.076923080.071428570.066666670.062500000.058823530.055555560.052631580.05000000
0.047619050.045454550.043478260.041666670.040000000.038461540.037037040.035714290.034482760.03333333
0.032258060.031250000.030303030.029411760.028571430.027777780.027027030.026315790.025641030.02500000
0.024390240.023809520.023255810.022727270.022222220.021739130.021276600.020833330.020408160.02000000
In [5]:
# Division
mat/2
Out[5]:
0.51.01.52.02.53.03.54.04.55.0
5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.510.0
10.511.011.512.012.513.013.514.014.515.0
15.516.016.517.017.518.018.519.019.520.0
20.521.021.522.022.523.023.524.024.525.0
In [8]:
# Power
mat ^ 2
Out[8]:
1 4 9 16 25 36 49 64 81100
121144169196225256289324361400
441484529576625676729784841900
961102410891156122512961369144415211600
1681176418491936202521162209230424012500

Comparison operators with matrices

We can similarly perform comparison operations across an entire matrix to return a matrix of logicals:

In [9]:
mat > 17
Out[9]:
FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
FALSEFALSEFALSEFALSEFALSEFALSEFALSE TRUE TRUE TRUE
TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE
TRUETRUETRUETRUETRUETRUETRUETRUETRUETRUE

Matrix Arithmetic with multiple matrices

We can use multiple matrices with arithmetic as well, for example:

In [13]:
mat + mat
Out[13]:
2 4 6 8101214161820
22242628303234363840
42444648505254565860
62646668707274767880
82 84 86 88 90 92 94 96 98100
In [14]:
mat / mat
Out[14]:
1111111111
1111111111
1111111111
1111111111
1111111111
In [16]:
# Warning, big numbers!
mat ^ mat
Out[16]:
1 4 27 256 3125 46656 823543 16777216 38742048910000000000
2.853117e+118.916100e+123.028751e+141.111201e+164.378939e+171.844674e+198.272403e+203.934641e+221.978420e+241.048576e+26
5.842587e+273.414279e+292.088047e+311.333736e+338.881784e+346.156120e+364.434265e+383.314552e+402.567686e+422.058911e+44
1.706917e+461.461502e+481.291100e+501.175664e+521.102507e+541.063874e+561.055513e+581.075912e+601.125951e+621.208926e+64
1.330878e+661.501309e+681.734377e+702.050774e+722.480636e+743.068035e+763.877924e+785.007021e+806.600972e+828.881784e+84
In [17]:
mat*mat
Out[17]:
1 4 9 16 25 36 49 64 81100
121144169196225256289324361400
441484529576625676729784841900
961102410891156122512961369144415211600
1681176418491936202521162209230424012500

Matrix multiplication

A quick side note on matrix multiplications. You can perform arithmetic multiplication on an element by element basis using the * sign in R. You should note this is not the same as Matrix Multiplication. If you are familiar with the mathematics behind this topic and want to use R to perform true matrix multiplication, you can use the following:

In [18]:
mat2 <- matrix(1:9, nrow=3)
In [19]:
mat2
Out[19]:
147
258
369
In [20]:
# True Matrix Multiplication
mat2 %*% mat2
Out[20]:
30 66102
36 81126
42 96150

Okay, that's it for matrices! Later on we will learn how to use factor() to quickly create categoical matrices from character elements.